#!/bin/bash
#############################
##一键安装lnmp
#############################

starttime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s);

#shell颜色变量
RED_COLOR='\E[1;31m'
GREEN_COLOR="\E[1;32m"
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
PINK_COLOR='\E[1;35m'
RES='\E[0m'

#引入公共自定义函数方法
wget https://gitee.com/guer168/dockerfiles/raw/master/func.sh
chmod +x func.sh
source ./func.sh

#获取系统版本号判断是否支持
system_version=$(cat /etc/redhat-release | grep release | awk '{print $4}')
OLD_IFS="$IFS"
IFS="."
version_array=($system_version)
IFS="$OLD_IFS"
if [ ${version_array[0]} != "8" ] ; then
	echo "$(pwd)必须centos8系统才能使用本脚本！"; exit
fi

#安装选择
echo -e "${GREEN_COLOR} 0.全部 1.nginx 2.mysql 3.php 4.redis ${RES}"
read -p " 选择需要安装的功能：" install_type


#创建软件下载目录  
mkdir -p  /home/soft && cd /home/soft

#更新并安装编译依赖相关软件
yum -y update
yum clean all && yum makecache
systemctl disable systemd-logind
systemctl mask systemd-logind
yum -y install make gcc gcc-c++ automake autoconf git vim wget
yum -y install libstdc++.so.6 curl curl-devel openssl-devel  kernel-devel net-tools psmisc initscripts

#关闭coredump
if [ -f "/etc/profile" ]; then
	if [ `grep -c "ulimit -S -c 0 > /dev/null 2>&1" /etc/profile` -eq 0 ] ; then
		echo -e "ulimit -S -c 0 > /dev/null 2>&1" >> /etc/profile && source /etc/profile
	fi
fi


#设置vim中文乱码
installed_vim=`whereis vim`
if [ "$installed_vim" != "vim:" ] ; then
	if [ -f "~/.vimrc" ];then
		if [ `grep -c "set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936" ~/.vimrc` -eq 0 ] ; then
			echo -e "set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936" >> ~/.vimrc
		fi
		if [ `grep -c "set termencoding=utf-8" ~/.vimrc` -eq 0 ] ; then
			echo -e "set termencoding=utf-8" >> ~/.vimrc
		fi
		if [ `grep -c "set encoding=utf-8" ~/.vimrc` -eq 0 ] ; then
			echo -e "set encoding=utf-8" >> ~/.vimrc
		fi
	else
		echo -e "set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936" >> ~/.vimrc
		echo -e "set termencoding=utf-8" >> ~/.vimrc
		echo -e "set encoding=utf-8" >> ~/.vimrc
	fi
fi

#设置北京时间同步
rm -rf /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
if [ ! -f "wlnmp-release-centos.noarch.rpm" ];then
	wget --no-check-certificate http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
fi
rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
yum -y install wntp
rpm -Uvh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
ntpdate time.nist.gov

#安装nginx
if [ "$install_type" = "1" ] || [ "$install_type" = "0" ] ; then
	yum -y install zlib zlib-devel openssl openssl-devel pcre-devel epel-release libxml2 libxslt-devel gd gd-devel perl-ExtUtils-Embed
	#卸载apache
	installed_httpd=`whereis httpd`
	if [ "$installed_httpd" != "httpd:" ] ; then
		systemctl disable httpd
		systemctl stop httpd
		yum -y remove httpd
		rm -f /usr/lib64/httpd && rm -rf /etc/httpd
	fi
	#卸载nginx
	typeset -l reinstall_nginx
	reinstall_nginx="y"
	start_install_nginx="y"
	installed_nginx=`whereis nginx`
	if [ "$installed_nginx" != "nginx:" ] ; then
		read -p " 已经安装Nginx是否重装(Y/n)：" reinstall_nginx
		#转换输入yes=>y,no=>n
		reinstall_nginx="${reinstall_nginx/yes/y}"
		reinstall_nginx="${reinstall_nginx/no/n}"
	fi
	if [ "$reinstall_nginx" = "y" ] ; then
		systemctl disable nginx
		systemctl stop nginx
		yum -y remove nginx
		rm -rf /usr/sbin/nginx && rm -rf /etc/nginx && rm -rf /etc/init.d/nginx && rm -rf /usr/local/nginx && rm -rf /usr/lib64/nginx && rm -rf /usr/share/nginx
		start_install_nginx="y"
	else
		start_install_nginx="n"
	fi
	
	if [ "$start_install_nginx" = "y" ] ; then
		#检查用户组
		cat /etc/group | grep nginx
		cat /etc/passwd | grep nginx
		groupadd nginx
		useradd nginx -g nginx -s /sbin/nologin -M
		mkdir -p /var/run/nginx && touch /var/run/nginx && chown -R nginx:nginx /var/run/nginx
		#编译安装
		if [ ! -f "nginx-1.18.0.tar.gz" ];then
			wget http://nginx.org/download/nginx-1.18.0.tar.gz
		fi
		tar -zxvf nginx-1.18.0.tar.gz
		cd nginx-1.18.0
		make clean
		./configure \
		--prefix=/usr/local/nginx \
		--user=nginx \
		--group=nginx \
		--with-file-aio \
		--with-http_ssl_module \
		--with-http_v2_module \
		--with-http_realip_module \
		--with-http_addition_module \
		--with-http_xslt_module=dynamic \
		--with-http_image_filter_module=dynamic \
		--with-http_sub_module \
		--with-http_dav_module \
		--with-http_flv_module \
		--with-http_mp4_module \
		--with-http_gunzip_module \
		--with-http_gzip_static_module \
		--with-http_random_index_module \
		--with-http_secure_link_module \
		--with-http_degradation_module \
		--with-http_slice_module \
		--with-http_stub_status_module \
		--with-http_perl_module=dynamic \
		--with-http_auth_request_module \
		--with-mail=dynamic \
		--with-mail_ssl_module \
		--with-pcre \
		--with-pcre-jit \
		--with-stream=dynamic \
		--with-stream_ssl_module \
		--with-debug
		#查看配置结果
		config_nginx=`echo $?`
		if [ "$config_nginx" != "0" ] ; then
			echo "$(pwd)配置Nginx出错"; exit
		fi
		make && make install
		#查看编译安装结果
		install_nginx=`echo $?`
		if [ "$install_nginx" != "0" ] ; then
			echo "$(pwd)编译安装Nginx出错"; exit
		fi
		#设置systemclt启动脚本
		wget https://gitee.com/guer168/dockerfiles/raw/master/conf/nginx/nginx.service
		mv nginx.service /usr/lib/systemd/system/
		chmod +x /usr/lib/systemd/system/nginx.service
		#设置虚拟主机配置
		mkdir -p /etc/nginx/conf.d/
		#引入扩展配置
		sed -i 'N;116a\\tinclude /etc/nginx/conf.d/*.conf;' /usr/local/nginx/conf/nginx.conf
		#写入demophp.conf
		wget https://gitee.com/guer168/dockerfiles/raw/master/conf/nginx/demophp.conf
		mv demophp.conf /etc/nginx/conf.d/
		#写入demoproxy.conf
		wget https://gitee.com/guer168/dockerfiles/raw/master/conf/nginx/demoproxy.conf
		mv demoproxy.conf /etc/nginx/conf.d/
		#设置启动
		systemctl daemon-reload
		systemctl start nginx
		systemctl enable nginx
		#删除解压的软件
		cd ../
		rm -rf nginx-1.18.0
	fi
fi


#安装mysql
if [ "$install_type" = "2" ] || [ "$install_type" = "0" ] ; then
	#卸载MariaDb
	installed_mariadb=`whereis mariadb`
	if [ "$installed_mariadb" != "mariadb:" ] ; then
		yum -y remove mariadb*  MariaDB*
		rm -rf /usr/local/mariadb /etc/my.cnf /etc/my.cnf.d /var/lib/mysql /etc/systemd/system/mariadb.service
	fi
	#卸载mysql
	installed_mysqld=`whereis mysqld`
	typeset -l reinstall_mysqld
	reinstall_mysqld="y"
	start_install_mysqld="y"
	if [ "$installed_mysqld" != "mysqld:" ] ; then
		read -p " 已经安装Mysql是否重装(Y/n)：" reinstall_mysqld
		#转换输入yes=>y,no=>n
		reinstall_mysqld="${reinstall_mysqld/yes/y}"
		reinstall_mysqld="${reinstall_mysqld/no/n}"
	fi
	if [ "$reinstall_mysqld" = "y" ] ; then
		systemctl disable mysqld.service
		systemctl stop mysqld.service
		rpm -qa | grep -i mysql | xargs rpm -e --noscripts
		yum -y remove mysql*
		rm -rf /usr/local/mysql /etc/my.cnf /etc/my.cnf.d /var/lib/mysql /etc/systemd/system/mysqld.service
		start_install_mysqld="y"
	else
		start_install_mysqld="n"
	fi
	
	if [ "$start_install_mysqld" = "y" ] ; then
		echo -e "${GREEN_COLOR} 1.mysql5.6 2.mysql5.7 3.mysql8.0 ${RES}"
		read -p " 选择数据库版本：" install_mysqld_version
		
		if [ "$install_mysqld_version" = "1" ] ; then
			#安装Mysql5.6
			rpm -qa | grep -i mysql | xargs rpm -e --noscripts
			if [ ! -f "mysql-community-release-el7-5.noarch.rpm" ];then
				wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
			fi
			rpm -ivh mysql-community-release-el7-5.noarch.rpm
			yum -y install mysql-server
			rpm -Uvh mysql-community-release-el7-5.noarch.rpm
		elif [ "$install_mysqld_version" = "2" ] ; then
			#安装Mysql5.7
			rpm -qa | grep -i mysql | xargs rpm -e --noscripts
			if [ ! -f "mysql57-community-release-el7-11.noarch.rpm" ];then
				wget wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
			fi
			yum localinstall mysql57-community-release-el7-11.noarch.rpm
			yum -y install mysql-server
			rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
		elif [ "$install_mysqld_version" = "3" ] ; then
			#安装Mysql8.0
			rpm -qa | grep -i mysql | xargs rpm -e --noscripts
			if [ ! -f "mysql80-community-release-el7-3.noarch.rpm" ];then
				wget http://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
			fi
			rpm -ivh mysql80-community-release-el7-3.noarch.rpm
			yum -y install mysql-server
			rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
		fi
		#启动服务
		systemctl daemon-reload
		systemctl enable mysqld.service
		systemctl start mysqld.service
		#设置初始密码
		mysql_secure_installation
	fi
fi


#安装PHP
if [ "$install_type" = "3" ] || [ "$install_type" = "0" ] ; then
	echo -e "${GREEN_COLOR} 1.php5.6 2.php7.3 3.php8.0 ${RES}"
	read -p " 选择PHP版本：" install_php_version
	path="7.3"
	if [ "$install_php_version" = "1" ] ; then
		path="5.6"
		#安装依赖
		yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses openssl-devel gdbm-devel libXpm-devel libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c openssl openssl-devel bzip2 bzip2-devel
		yum -y install openldap openldap-devel epel-release net-snmp-devel
		yum -y install libmcrypt libmcrypt-devel mhash
		#解决：configure: error: Cannot find ldap libraries in /usr/lib
		cp -frp /usr/lib64/libldap* /usr/lib/
		ln -sf /usr/local/lib/libiconv.so.2 /usr/lib64/
		#解决：error adding symbols: DSO missing from command line
		pkg-config --print-requires --print-requires-private glfw3
		#安装libmcrypt
		if [ ! -f "atomic" ];then
			wget http://www.atomicorp.com/installers/atomic
		fi
		sh ./atomic
		#安装匹配版本openssl
		if [ ! -f "openssl-1.0.2k.tar.gz" ];then
			wget --no-check-certificate https://www.openssl.org/source/openssl-1.0.2k.tar.gz
		fi
		tar -zxvf openssl-1.0.2k.tar.gz
		cd openssl-1.0.2k
		./config --prefix=/usr/local/openssl --openssldir=/usr/local/ssl shared
		#查看配置结果
		config_openssl=`echo $?`
		if [ "$config_openssl" != "0" ] ; then
			echo "$(pwd)配置Openssl出错"; exit
		fi
		make && make install
		#查看编译安装结果
		install_openssl=`echo $?`
		if [ "$install_openssl" != "0" ] ; then
			echo "$(pwd)编译安装Openssl出错"; exit
		fi
		cd ../
		#下载
		if [ ! -f "php-5.6.40.tar.gz" ];then
			wget --no-check-certificate https://www.php.net/distributions/php-5.6.40.tar.gz
		fi
		tar -zxvf php-5.6.40.tar.gz
		cd php-5.6.40
		#配置
		make clean
		./configure \
		--prefix=/usr/local/php/5.6 \
		--with-config-file-path=/usr/local/php/5.6/etc \
		--with-fpm-user=nginx  \
		--with-fpm-group=nginx \
		--with-openssl=/usr/local/openssl \
		--enable-fpm \
		--with-mysql \
		--with-mysqli \
		--enable-mysqlnd \
		--with-mysqli=mysqlnd \
		--with-pdo-mysql=mysqlnd \
		--enable-sockets \
		--enable-mbstring \
		--enable-bcmath \
		--with-freetype-dir \
		--with-jpeg-dir \
		--with-png-dir \
		--with-zlib-dir \
		--with-libxml-dir=/usr \
		--with-pdo_mysql \
		--enable-xml \
		--with-mhash \
		--with-mcrypt \
		--with-bz2 \
		--with-curl \
		--with-gd \
		--with-gettext \
		--with-snmp \
		--with-ldap \
		--with-iconv \
		--enable-sysvsem \
		--enable-sysvshm \
		--enable-pcntl \
		--enable-mbregex \
		--enable-exif \
		--with-pcre-regex \
		--enable-ftp \
		--with-xmlrpc \
		--with-xsl \
		--enable-opcache \
		--with-pear
		#查看配置结果
		config_php=`echo $?`
		if [ "$config_php" != "0" ] ; then
			echo "$(pwd)配置php出错"; exit
		fi
		#解决：error adding symbols: DSO missing from command line
		#编辑Makefile，然后搜索EXTRA_LIBS =， EXTRA_LIBS = ..... -lcrypt 在最后加上 -llber，例如: EXTRA_LIBS = ..... -lcrypt -llber 然后重新再次 make 即可
		line_str=`grep -n "^EXTRA_LIBS =" Makefile`
		OLD_IFS="$IFS"
		IFS=":"
		arr=($line_str)
		IFS="$OLD_IFS"
		line_num=${arr[0]}
		old_str=${arr[1]}
		new_str="${old_str} -llber"
		sed -i "${line_num}s#${old_str}#${new_str}#" Makefile
		make && make install
		#查看编译安装结果
		install_php=`echo $?`
		if [ "$install_php" != "0" ] ; then
			echo "$(pwd)编译安装php出错"; exit
		fi
		#设置配置
		mkdir -p /usr/local/php/${path}/etc/php-fpm.d/
		cp sapi/fpm/php-fpm.conf /usr/local/php/${path}/etc/php-fpm.conf
		ln -sf /usr/local/php/${path}/etc/php-fpm.conf /usr/local/php/${path}/etc/php-fpm.d/www.conf
	elif [ "$install_php_version" = "2" ] ; then
		path="7.3"
		#安装依赖
		yum -y install libxml2 libXpm-devel libcurl-devel libjpeg-devel libpng-devel libicu-devel openldap-devel gd
		yum -y install libevent-devel libxslt-devel freetype-devel unixODBC-devel readline-devel
		yum -y install gmp-devel net-snmp-devel epel-release bzip2 bzip2-devel
		yum -y install php-cli php-process php-devel php-pear pcre-devel openssl openssl-devel libxml2
		#解决：error: Please reinstall the libzip distribution
		yum -y remove libzip
		if [ ! -f "libzip-1.3.2.tar.gz" ];then
			wget --no-check-certificate https://libzip.org/download/libzip-1.3.2.tar.gz
		fi
		tar zxf libzip-1.3.2.tar.gz
		cd libzip-1.3.2
		./configure
		#查看配置结果
		config_libzip=`echo $?`
		if [ "$config_libzip" != "0" ] ; then
			echo "$(pwd)配置libzip出错"; exit
		fi
		make && make install
		#查看编译安装结果
		install_libzip=`echo $?`
		if [ "$install_libzip" != "0" ] ; then
			echo "$(pwd)编译安装libzip出错"; exit
		fi
		cd ../
		#解决：error: off_t undefined; check your library configuration
		if [ -f "/etc/ld.so.conf" ];then
			if [ `grep -c "/usr/local/lib" /etc/ld.so.conf` -eq 0 ] ; then
				echo -e "/usr/local/lib" >> /etc/ld.so.conf
			fi
			if [ `grep -c "/usr/local/lib64" /etc/ld.so.conf` -eq 0 ] ; then
				echo -e "/usr/local/lib64" >> /etc/ld.so.conf
			fi
			if [ `grep -c "/usr/lib" /etc/ld.so.conf` -eq 0 ] ; then
				echo -e "/usr/lib" >> /etc/ld.so.conf
			fi
			if [ `grep -c "/usr/lib64" /etc/ld.so.conf` -eq 0 ] ; then
				echo -e "/usr/lib64" >> /etc/ld.so.conf
			fi
			ldconfig -v
		fi
		#下载
		if [ ! -f "php-7.3.26.tar.gz" ];then
			wget --no-check-certificate https://www.php.net/distributions/php-7.3.26.tar.gz
		fi
		tar -zxvf php-7.3.26.tar.gz
		cd php-7.3.26
		#配置
		make clean
		./configure \
		--prefix=/usr/local/php/7.3 \
		--with-config-file-path=/usr/local/php/7.3/etc \
		--enable-fpm \
		--with-fpm-user=nginx  \
		--with-fpm-group=nginx \
		--enable-inline-optimization \
		--disable-debug \
		--disable-rpath \
		--enable-shared  \
		--enable-soap \
		--with-libxml-dir \
		--with-xmlrpc \
		--with-openssl \
		--with-mhash \
		--enable-pcntl \
		--with-pcre-regex \
		--with-sqlite3 \
		--with-zlib \
		--enable-bcmath \
		--with-iconv \
		--with-bz2 \
		--enable-calendar \
		--with-curl \
		--with-cdb \
		--enable-dom \
		--enable-exif \
		--enable-filter \
		--with-pcre-dir \
		--enable-ftp \
		--with-gd \
		--with-openssl-dir \
		--with-jpeg-dir \
		--with-png-dir \
		--with-zlib-dir  \
		--with-freetype-dir \
		--with-gettext \
		--with-gmp \
		--enable-json \
		--enable-mbstring \
		--enable-mbregex \
		--enable-mbregex-backtrack \
		--with-libmbfl \
		--with-onig \
		--enable-pdo \
		--with-mysqli=shared,mysqlnd \
		--with-pdo-mysql=shared,mysqlnd \
		--with-pdo-sqlite \
		--with-readline \
		--enable-session \
		--enable-shmop \
		--enable-simplexml \
		--enable-sockets  \
		--enable-sysvmsg \
		--enable-sysvsem \
		--enable-sysvshm \
		--enable-wddx \
		--enable-xml \
		--with-xsl \
		--enable-zip \
		--with-snmp \
		--enable-mysqlnd-compression-support \
		--with-pear \
		--enable-opcache \
		--disable-fileinfo
		#查看配置结果
		config_php=`echo $?`
		if [ "$config_php" != "0" ] ; then
			echo "$(pwd)配置php出错"; exit
		fi
		make && make install
		#查看编译安装结果
		install_php=`echo $?`
		if [ "$install_php" != "0" ] ; then
			echo "$(pwd)编译安装php出错"; exit
		fi
		#设置配置
		cp /usr/local/php/${path}/etc/php-fpm.conf.default  /usr/local/php/${path}/etc/php-fpm.conf
		cp /usr/local/php/${path}/etc/php-fpm.d/www.conf.default  /usr/local/php/${path}/etc/php-fpm.d/www.conf
	elif [ "$install_php_version" = "3" ] ; then
		path="8.0"
		#安装依赖
		yum -y install libxml2 libXpm-devel libcurl-devel libjpeg-devel libpng-devel libicu-devel openldap-devel gd
		yum -y install libevent-devel libxslt-devel freetype-devel unixODBC-devel readline-devel
		yum -y install gmp-devel net-snmp-devel epel-release bzip2 bzip2-devel
		yum -y install php-cli php-process php-devel php-pear pcre-devel openssl openssl-devel libxml2 sqlite-devel
		#解决： error: Package requirements (oniguruma) were not met
		if [ ! -f "oniguruma-6.9.4.tar.gz" ]; then
			wget --no-check-certificate https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
		fi
		tar -zxf oniguruma-6.9.4.tar.gz
		cd oniguruma-6.9.4
		./autogen.sh && ./configure --prefix=/usr
		#查看配置结果
		config_oniguruma=`echo $?`
		if [ "$config_oniguruma" != "0" ]; then
			echo "$(pwd)配置oniguruma出错"; exit
		fi
		make && make install
		#查看编译安装结果
		install_oniguruma=`echo $?`
		if [ "$install_oniguruma" != "0" ] ; then
			echo "$(pwd)编译安装oniguruma出错"; exit
		fi
		cd ../
		#解决：error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip !=1.7.0) were not met
		yum -y remove libzip
		if [ ! -f "libzip-1.3.2.tar.gz" ];then
			wget --no-check-certificate https://libzip.org/download/libzip-1.3.2.tar.gz
		fi
		tar zxf libzip-1.3.2.tar.gz
		cd libzip-1.3.2
		./configure
		#查看配置结果
		config_libzip=`echo $?`
		if [ "$config_libzip" != "0" ] ; then
			echo "$(pwd)配置libzip出错"; exit
		fi
		make && make install
		#查看编译安装结果
		install_libzip=`echo $?`
		if [ "$install_libzip" != "0" ] ; then
			echo "$(pwd)编译安装libzip出错"; exit
		fi
		cd ../
		if [ -f "/etc/ld.so.conf" ];then
			if [ `grep -c "/usr/local/lib" /etc/ld.so.conf` -eq 0 ] ; then
				echo -e "/usr/local/lib" >> /etc/ld.so.conf
			fi
			if [ `grep -c "/usr/local/lib64" /etc/ld.so.conf` -eq 0 ] ; then
				echo -e "/usr/local/lib64" >> /etc/ld.so.conf
			fi
			if [ `grep -c "/usr/lib" /etc/ld.so.conf` -eq 0 ] ; then
				echo -e "/usr/lib" >> /etc/ld.so.conf
			fi
			if [ `grep -c "/usr/lib64" /etc/ld.so.conf` -eq 0 ] ; then
				echo -e "/usr/lib64" >> /etc/ld.so.conf
			fi
			ldconfig -v
			export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/:$PKG_CONFIG_PATH
		fi
		#下载
		if [ ! -f "php-8.0.7.tar.gz" ];then
			wget --no-check-certificate https://www.php.net/distributions/php-8.0.7.tar.gz
		fi
		tar -zxvf php-8.0.7.tar.gz
		cd php-8.0.7
		#配置
		make clean
		./configure \
		--prefix=/usr/local/php/8.0 \
		--with-config-file-path=/usr/local/php8.0/etc \
		--with-fpm-user=nginx \
		--with-fpm-group=nginx \
		--enable-fpm \
		--with-curl \
		--with-freetype \
		--enable-gd \
		--with-jpeg \
		--with-gettext \
		--with-kerberos \
		--with-libdir=lib64 \
		--with-libxml \
		--with-mysqli \
		--with-openssl \
		--with-pdo-mysql \
		--with-pdo-sqlite \
		--enable-sockets \
		--with-mhash \
		--with-ldap-sasl \
		--with-xsl \
		--with-zlib \
		--with-zip \
		--with-bz2 \
		--with-iconv \
		--enable-pdo \
		--enable-bcmath \
		--enable-mbregex \
		--enable-mbstring \
		--enable-pcntl \
		--enable-shmop \
		--enable-soap \
		--enable-sysvsem \
		--enable-xml \
		--enable-cli \
		--enable-intl \
		--enable-calendar \
		--enable-static \
		--enable-mysqlnd \
		--enable-opcache \
		--with-pear
		#查看配置结果
		config_php=`echo $?`
		if [ "$config_php" != "0" ] ; then
			echo "$(pwd)配置php出错"; exit
		fi
		make && make install
		#查看编译安装结果
		install_php=`echo $?`
		if [ "$install_php" != "0" ] ; then
			echo "$(pwd)编译安装php出错"; exit
		fi
		#设置配置
		cp /usr/local/php/${path}/etc/php-fpm.conf.default  /usr/local/php/${path}/etc/php-fpm.conf
		cp /usr/local/php/${path}/etc/php-fpm.d/www.conf.default  /usr/local/php/${path}/etc/php-fpm.d/www.conf
	fi
	#设置配置文件
	cp php.ini-production  /usr/local/php/${path}/etc/php.ini
	cp sapi/fpm/init.d.php-fpm  /etc/init.d/php${path}-fpm
	chmod +x /etc/init.d/php${path}-fpm
	#设置可以直接用php -v式命令
	ln -sf /usr/local/php/${path}/bin/php /usr/local/sbin/php${path}
	typeset -l set_main_php
	set_main_php='y'
	read -p " 是否设置为主版本(Y/n)：" set_main_php
	if [ "$set_main_php" = "" ]; then
		set_main_php="y"
	fi
	#转换输入yes=>y,no=>n
	set_main_php="${set_main_php/yes/y}"
	set_main_php="${set_main_php/no/n}"
	if [ "$set_main_php" = "y" ]; then
		ln -sf /usr/local/php/${path}/bin/php /usr/local/sbin/php
	fi
	#设置使用端口
	while :
	do
		read -p " 设置使用端口(默认:9000):" set_port
		if [ "$set_port" = "" ]; then
			set_port="9000"
		fi
		used_port1=`find /usr/local/ -path ./php/${path} -type f -name "www.conf" | xargs grep "^listen = 127.0.0.1:${set_port}"`
		used_port2=`find /usr/local/ -path ./php/${path} -type f -name "www.conf" | xargs grep "^listen = 0.0.0.0:${set_port}"`
		if [ "$used_port1" = "" ] && [ "$used_port2" = "" ]; then
			break
		else
			echo "${set_port}已被使用"
		fi
	done
	#设置使用端口
	cp /usr/local/php/${path}/etc/php-fpm.d/www.conf /usr/local/php/${path}/etc/php-fpm.d/www.conf.bak
	if [ "$path" = "5.6" ]; then
		str_replace_file "^listen = 0.0.0.0:9000" "listen = 0.0.0.0:${set_port}" /usr/local/php/${path}/etc/php-fpm.conf
		str_replace_file "^listen = 127.0.0.1:9000" "listen = 0.0.0.0:${set_port}" /usr/local/php/${path}/etc/php-fpm.conf
	else
		str_replace_file "^listen = 0.0.0.0:9000" "listen = 0.0.0.0:${set_port}" /usr/local/php/${path}/etc/php-fpm.d/www.conf
		str_replace_file "^listen = 127.0.0.1:9000" "listen = 0.0.0.0:${set_port}" /usr/local/php/${path}/etc/php-fpm.d/www.conf
	fi
	#设置php.ini配置
	cp /usr/local/php/${path}/etc/php.ini /usr/local/php/${path}/etc/php.ini.bak
	str_replace_file "^;date.timezone =" "date.timezone = \"Asia/Shanghai\"" /usr/local/php/${path}/etc/php.ini
	str_replace_file "^;cgi.fix_pathinfo=1" "cgi.fix_pathinfo=1" /usr/local/php/${path}/etc/php.ini
	#设置PHP开机启动
	chmod +x /etc/init.d/php${path}-fpm
	chkconfig --add php${path}-fpm    #加入开机启动
	chkconfig php${path}-fpm on
	service php${path}-fpm start
	#安装composer
	if [ ! -f "/usr/local/bin/composer" ];then
		curl -sS https://getcomposer.org/installer | php
		mv composer.phar /usr/local/bin/composer
		chmod +x /usr/local/bin/composer
	fi
	cd ../
fi


#安装Redis
if [ "$install_type" = "4" ] || [ "$install_type" = "0" ]; then
	installed_redis=`whereis redis`
	typeset -l reinstall_redis
	reinstall_redis="y"
	start_install_redis="y"
	if [ "$installed_redis" != "redis:" ]; then
		read -p " 已经安装Redis是否重装(Y/n)：" reinstall_mysql
		#转换输入yes=>y,no=>n
		reinstall_redis="${reinstall_redis/yes/y}"
		reinstall_redis="${reinstall_redis/no/n}"
	fi
	if [ "$reinstall_mysql" = "y" ]; then
		systemctl disable redis.service
		systemctl stop redis.service
		yum -y remove redis
		start_install_redis="y"
	else
		start_install_redis="n"
	fi
	if [ "$start_install_redis" = "y" ]; then
		yum -y install redis
		systemctl daemon-reload
		systemctl enable redis
		systemctl start redis
	fi
fi


#删除所有解压文件夹
#ls -l |awk '/^d/ {print $NF}' | xargs rm -rf && rm -rf *.rpm.*
#统计运行耗时
rm -rf ./func.sh
endtime=`date +'%Y-%m-%d %H:%M:%S'`
end_seconds=$(date --date="$endtime" +%s);
echo "$starttime ---> $endtime" "全部安装完成,总共耗时:"$((end_seconds-start_seconds))"秒"